﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _32_C_中ref引用传递和ref返回值
{
    internal class PersonCollection
    {
        private Person[] peoples = {
            new Person(){
            Id = 1,
            Name="张三"
            },
            new Person(){
            Id = 2,
            Name="李四"
            }
        };
        private Person person1 = null;

        public Person GetPerson(string name)
        {
            foreach (Person person in peoples)
            {
                if (person.Name == name)
                {
                    return person;
                }
            }
            return person1;
        }

        public ref Person GetPersonref(string name) //返回引用
        {
            for (int i = 0; i < peoples.Length; i++)
            {
                if (peoples[i].Name == name)
                {
                    return ref peoples[i];
                }
            }
            return ref person1;
        }

        public void Show()
        {
            foreach (Person person in peoples)
            {
                Console.WriteLine($"id:{person.Id}-name:{person.Name}");
            }
        }
    }
}
